package com.hou.postgresql.config;

import com.baomidou.dynamic.datasource.plugin.MasterSlaveAutoRoutingPlugin;
import com.baomidou.mybatisplus.core.parser.ISqlParser;
import com.baomidou.mybatisplus.extension.parsers.BlockAttackSqlParser;
import com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;

@Configuration
public class MyBtaisPlusConfig {

    /**
     * 分页插件
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        List<ISqlParser> sqlParserList = new ArrayList<>();
        // 攻击 SQL 阻断解析器、加入解析链
        sqlParserList.add(new BlockAttackSqlParser());
        paginationInterceptor.setSqlParserList(sqlParserList);
        //防止全表delete或者update, 比如  service.delete(null)  全表删除
        return paginationInterceptor;
    }

    /**
     * SQL执行效率插件
     * //@Profile({"dev","test"})// 设置 dev test 环境开启
//     */
//    @Bean
//    public PerformanceInterceptor performanceInterceptor() {
//        PerformanceInterceptor performanceInterceptor = new PerformanceInterceptor();
//        // 执行最大时间,超过自动停止
//        performanceInterceptor.setMaxTime(1000);
//        // sql是否格式化
//        performanceInterceptor.setFormat(true);
//        return performanceInterceptor;
//    }

    /**
     * 纯读写分离默认配置,不用@DS注解手动切换, 读master数据源,写slave数据源,名字必须写死
     * @return
     */
    @Bean
    public MasterSlaveAutoRoutingPlugin masterSlaveAutoRoutingPlugin(){
        return new MasterSlaveAutoRoutingPlugin();
    }


    /**
     * 乐观锁插件,注解实体字段 @Version 必须要,
     * 仅支持 updateById(id) 与 update(entity, wrapper) 方法
     * @return
     */
    @Bean
    public OptimisticLockerInterceptor optimisticLockerInterceptor() {
        return new OptimisticLockerInterceptor();
    }
}
